package org.sport.dbImplement;

import org.sport.dbInterface.LinManOperation;
import org.sport.dbInterface.UserOperation;
import org.sport.model.entityModel.LinkmantableEntity;
import org.sport.model.entityModel.UsertableEntity;
import org.sport.repository.LinkmantableRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;


import java.util.List;

/**
 * Created by Allen on 2017/9/22.
 */
@Service
public class LinkManOpeImp implements LinManOperation {

    @Autowired
    LinkmantableRepository  linManRepository;

    @Autowired
    UserOpeImp              userOpeImp;

    /**
     * 增
     * @param ownerId
     * @param linkMan
     * @param: 拥有者id
     * @param: 联系人id
     */
    @Override
    public boolean addOneLinkMan(UsertableEntity ownerId, UsertableEntity linkMan) {

        if(ownerId != null && linkMan != null)
        {
            LinkmantableEntity  linkmantableEntity = new LinkmantableEntity();
            linkmantableEntity.setUsertableByUserId(linkMan);
            linkmantableEntity.setUsertableByOwnerId(ownerId);
            linManRepository.saveAndFlush(linkmantableEntity);
            return true;
        }
        else
            return false;
    }

    /**
     * 改     改备注？有需求再加
     */
    @Override
    public boolean updateOne() {
        return false;
    }

    @Override
    public boolean del(UsertableEntity ownerId, UsertableEntity linkMan) {

        if(ownerId != null && linkMan != null) {
            LinkmantableEntity linkmantableEntity = new LinkmantableEntity();
            linkmantableEntity = linManRepository.findOne(ownerId, linkMan);
            linManRepository.delete(linkmantableEntity);
            return true;
        }
        else
            return false;
    }

    @Override
    public LinkmantableEntity findOneLinkMan(UsertableEntity ownerId,UsertableEntity linkMan) {
        return linManRepository.findOne(ownerId, linkMan);
    }

    @Override
    public List<UsertableEntity> findUserLinkMan(UsertableEntity ownerId)
    {
        List<UsertableEntity> userList = new ArrayList<UsertableEntity>();
        List<LinkmantableEntity> linkmanList = linManRepository.findUserList(ownerId);
        for (int i = 0;i < linkmanList.size();i++)
        {
            userList.add(userOpeImp.findUserById(linkmanList.get(i).getUsertableByUserId().getId()));
        }

        return userList;
    }

    @Override
    public List<UsertableEntity> findAllLinkMan() {
        return null;
    }
}
